const clarityInit = function (id) {
    (function (c, l, a, r, i, t, y) {
        c[a] =
            c[a] ||
            function () {
                (c[a].q = c[a].q || []).push(arguments);
            };
        t = l.createElement(r);
        t.async = 1;
        t.src = "https://www.clarity.ms/tag/" + i;
        y = l.getElementsByTagName(r)[0];
        y.parentNode.insertBefore(t, y);
    })(window, document, "clarity", "script", id);
};

export function clarity() {
    var params = Array.prototype.slice.call(arguments);
    if (!window.clarity) {
        throw new Error("Clarity is not initialized");
    }

    window.clarity.apply(undefined, params);
}

export function init(id) {
    clarityInit(id);
}

export function hasStarted() {
    return typeof window.clarity === "function";
}

export function identify(userId, properties) {
    clarity("identify", userId, properties);
}

export function consent() {
    clarity("consent");
}

export function setTag(key, value) {
    clarity("set", key, value);
}

export function upgrade(reason) {
    clarity("upgrade", reason);
}